<?php


if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class m_diagnosa extends CI_Model {


	function ambil($jenis) {
		$this->db->select('*');
		$this->db->from('penyakit');
		$this->db->group_by('penyakit.id_penyakit');
		$this->db->join('bobot_gejala', 'penyakit.id_penyakit=bobot_gejala.id_penyakit');
		$this->db->where('bobot_gejala.jenishewan', $jenis);
		$ambil = $this->db->get();
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}

	function hapus($jenis, $id_penyakit) {
		if($jenis == 'sapi'){
			$jenis = 1;
		}
		else if($jenis == 'kambing'){
			$jenis = 2;
		}
		else if($jenis == 'domba'){
			$jenis = 3;
		}
		$this->db->trans_start();
		$this->db->delete('bobot_gejala', array('jenishewan' => $jenis, 'id_penyakit' => $id_penyakit));
		$this->db->trans_complete();
		return;
	}


	function hapus_gejala($jenis, $id_bobot_gejala) {
		if($jenis == 'sapi'){
			$jenis = 1;
		}
		else if($jenis == 'kambing'){
			$jenis = 2;
		}
		else if($jenis == 'domba'){
			$jenis = 3;
		}
		$this->db->trans_start();
		$this->db->delete('bobot_gejala', array('jenishewan' => $jenis, 'id_bobot_gejala' => $id_bobot_gejala));
		$this->db->trans_complete();
		return;
	}


//        function ambilgejalabyjenis($jenis) {
//		$this->db->select('*');
//		$this->db->from('gejala');
//		$this->db->join('bobot_gejala', 'gejala.id_gejala=bobot_gejala.id_gejala');
//		$this->db->where('bobot_gejala.jenishewan', $jenis);
//		$ambil = $this->db->get();
//		if ($ambil->num_rows() > 0) {
//			foreach ($ambil->result() as $data) {
//				$hasil[] = $data;
//			}
//			return $hasil;
//		}
//	}
	

	function ambilgejalabyjenis() {
		$this->db->select('*');
		$this->db->from('gejala');
//		$this->db->join('bobot_gejala', 'gejala.id_gejala=bobot_gejala.id_gejala');
//		$this->db->where('bobot_gejala.jenishewan', $jenis);
		$ambil = $this->db->get();
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}

	function bobotgejala() {
		$this->db->select('*');
		$this->db->from('bobot_gejala');
//		$this->db->join('bobot_gejala', 'gejala.id_gejala=bobot_gejala.id_gejala');
//		$this->db->where('bobot_gejala.jenishewan', $jenis);
		$ambil = $this->db->get();
		if ($ambil->num_rows() > 0) {
			foreach ($ambil->result() as $data) {
				$hasil[] = $data;
			}
			return $hasil;
		}
	}



	function gejala_by_kd($kd_gejala){
		$ambil = $this->db->get_where('gejala', array('kd_gejala'=>$kd_gejala));
		return $ambil->result_array();
	}
	function penyakit_by_kd($kd_penyakit){
		$ambil = $this->db->get_where('penyakit', array('kd_penyakit'=>$kd_penyakit));
		return $ambil->result_array();
	}

	function cek($id_gejala="", $id_penyakit="",$jenishewan){
		$this->db->where('id_penyakit', $id_penyakit);
		$this->db->where('id_gejala', $id_gejala);
		$this->db->where('jenishewan', $jenishewan);
		$ambil = $this->db->get('bobot_gejala');
		if($ambil->num_rows > 0){
			return true;
		}else{
			return false;
		}
	}

	function insertbobot(){
		$id_gejala = $this->input->post('id_gejala');
		$id_penyakit = $this->input->post('id_penyakit');
		$bobot = $this->input->post('bobot');
		$jenishewan = $this->input->post('jenishewan');
		$data  = array('id_penyakit' => $id_penyakit, 'id_gejala' => $id_gejala, 'bobot' => $bobot, 'jenishewan' => $jenishewan );
		if($this->cek($id_gejala, $id_penyakit, $jenishewan) == false) {
			$this->db->trans_start();
			$this->db->insert('bobot_gejala', $data);
			$this->db->trans_complete();
			$this->session->set_flashdata('result_pass_true', 'Data Klasifikasi Diagnosa berhasil');
		} else{
			$this->session->set_flashdata('result_pass', 'Kode Gejala sudah pernah digunakan');
			header('location:' .site_url('diagnosa/klasifikasi'));
		}
		header('location:' . site_url('diagnosa/klasifikasi'));

	}
}